<?php

namespace SL\BackBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

use SL\FrontBundle\Form\ImageProjetType;
use SL\FrontBundle\Entity\ImageProjet;
use SL\FrontBundle\Entity\ImagePresse;
use SL\FrontBundle\Entity\Image;

class ImagesController extends Controller
{	
	public function projetAddAction($id)
	{
		
		
		$em = $this->getDoctrine()->getManager();
		$projet = $em->getRepository('SLFrontBundle:Projet')->findOneBy(array('id' => $id));
		if ($projet === null)
			throw $this->createNotFoundException('Projet: '.$id.' inexistant.');
		
		$form = $this->createForm(new ImageProjetType);
		
		$request = $this->get('request');
		if ($request->getMethod() == 'POST')
		{
			$form->handleRequest($this->getRequest());
			
			if ($form->isValid())
			{
				$data = $form->getData();
				$last = $em->getRepository('SLFrontBundle:ImageProjet')->findOneBy(array('projet' => $projet), array('position' => "DESC"));
				if ($last === null)
					$pos = 0;
				else
					$pos = $last->getPosition();
				
				for ($i = 1; $i <= 10; $i++)
				{
					if ($form["small" . $i]->getData() !== null &&
						$form["big" . $i]->getData() !== null)
					{
					$image = new ImageProjet();
					$image->setPosition($i + $pos);
					$image->setProjet($projet);
					$image->setSmall(new Image());
					$image->getSmall()->setUploadDir("small");
					$image->getSmall()->file = $form["small" . $i]->getData();
					$image->setBig(new Image());
					$image->getBig()->setUploadDir("big");
					$image->getBig()->file = $form["big" . $i]->getData();
					$em->persist($image);
					}
				}
				$em->flush();
				
					
				return $this->redirect($this->generateUrl('sl_back_projet_edit', array('url' => $image->getProjet()->getUrl())));
			}
		}
		
		return $this->render('SLBackBundle:Image:add.html.twig', 
							array('projet' => $projet, 'form' => $form->createView()));
	}
	
	public function projetEditAction($id)
	{
		$repository = $this->getDoctrine()->getManager()->getRepository('SLFrontBundle:ImageProjet');
		
		$image = $repository->findOneBy(array('id' => $id));
		if ($image === null)
		{
			throw $this->createNotFoundException('Image '.$id.' inexistante.');
		}
		
		$form = $this->createForm(new ImageProjetType, $image);
		
		$request = $this->get('request');
		if ($request->getMethod() == 'POST')
		{
			$form->bind($request);
			
			if ($form->isValid())
			{
				$em = $this->getDoctrine()->getManager();
				$em->flush();
				
				return $this->redirect($this->generateUrl('sl_back_projet_edit', array('url' => $image->getProjet()->getUrl())));
			}
		}
				
		return $this->render('SLBackBundle:Image:edit.html.twig', 
						array('image' => $image, 'form' => $form->createView()));
	}
	
	public function projetDeleteAction($id)
	{
		$repository = $this->getDoctrine()->getManager()->getRepository('SLFrontBundle:ImageProjet');
		$repoProjet = $this->getDoctrine()->getManager()->getRepository('SLFrontBundle:Projet');
		
		$image = $repository->findOneBy(array('id' => $id));		
		if ($image === null)
		{
			throw $this->createNotFoundException('Image '.$id.' inexistante.');
		}
		
		$projet = $repoProjet->findOneBy(array('id' => $image->getProjet()));
		if ($projet === null)
		{
			throw $this->createNotFoundException('Le projet ' .$image->getProjet().' de l\'image '.$id.' est inexistant.');
		}
		
		$form = $this->createFormBuilder()->getForm();
		
		$request = $this->get('request');
		if ($request->getMethod() == 'POST')
		{
			$form->bind($request);
			
			$em = $this->getDoctrine()->getManager();
			$em->remove($image);
			$em->flush();
				
			$this->get('session')->getFlashBag()->add('Administration', 'L\'image '.$image->getWebPath().' a bien été supprimé');
				
			return $this->redirect($this->generateUrl('sl_back_projet_edit', array('url' => $projet->getUrl())));
			
		}
				
		return $this->render('SLBackBundle:Image:delete.html.twig', 
									array('image' => $image, 'form' => $form->createView(), 'url' => $projet->getUrl()));
	}
}